//
//  YWMonitorAlarmManager.m
//  maintain
//
//  Created by 烟雨云渊 on 17/1/10.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWMonitorAlarmManager.h"
#import "YWMonitorAlarmDBManager.h"
#import "WMHttpTool.h"
#import "YWCommonTool.h"
#import "YWMacro.h"
#import "YWConst.h"
#import "MJExtension.h"
#import "YWMonitorAlarmModel.h"
#import "YWLogRecordTool.h"

@interface YWMonitorAlarmManager ()
@end

@implementation YWMonitorAlarmManager
YWSingletonM(Manager)

- (BOOL)updateMonitorAlarmWithArray:(NSArray*)MonitorAlarmArray{
    return [[YWMonitorAlarmDBManager shareManager] updateMonitorAlarmWithArray:MonitorAlarmArray];
}

- (NSArray*)monitorAlarmArrayWithPage:(NSInteger)page{
    return [[YWMonitorAlarmDBManager shareManager] monitorAlarmArrayWithPage:page];
}

- (BOOL)updateMonitorFieldWithArray:(NSArray*)fieldArray{
    return [[YWMonitorAlarmDBManager shareManager] updateMonitorFieldWithArray:fieldArray];
}

- (void)requestMonitorAlarmWithPage:(NSInteger)page pageSize:(NSInteger)pageSize WithComplete:(void (^)(BOOL))complete{
    NSDictionary *headers = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headers;
    if (headers == nil) {if (complete) {complete(NO);}return;}
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
//    if (pullTime) {
//        [params setValue:@(pullTime) forKey:@"pullTime"];
//    }
    if (page) {
        [params setValue:@(page) forKey:@"page"];
    }
    if (pageSize) {
        [params setValue:@(pageSize) forKey:@"pageSize"];
    }
    NSString *url = [YWCommonTool getWarnUrl];
    YWLog(@"url -- %@,%@",url,params);
    //保存请求前的参数日志
    NSString *logTag = [YWCommonTool randStringId];
    NSMutableDictionary * logDic = [[YWLogRecordTool shareLogRecordManager] getLogDictionaryWithModule:@"监控报警" interface:url params:params headers:headers tag:logTag];
    [[YWLogRecordTool shareLogRecordManager] saveLocalPatameterLog:logDic];
    
    [[WMHttpTool shareHttpTool] getWithUrl:url params:params success:^(NSHTTPURLResponse* response, id json) {
        NSArray* monitorArray = [YWMonitorAlarmModel mj_objectArrayWithKeyValuesArray:json];
        [[YWMonitorAlarmManager shareManager] updateMonitorAlarmWithArray:monitorArray];
        //保存请求成功的参数日志
        NSMutableDictionary * successDic =
        [[YWLogRecordTool shareLogRecordManager] successOrFailedLogDicWith:logDic statusCode:response.statusCode errorInfo:@"无" logTag:logTag];
        [[YWLogRecordTool shareLogRecordManager] saveLocalPatameterLog:successDic];
        
        if (complete) {
            complete(YES);
        }
    } failure:^(NSHTTPURLResponse* response, NSError* error) {
        
        //保存请求失败的参数日志
        NSString * result = [NSString stringWithFormat:@"%s,error:%@", __func__, error.localizedDescription];
        NSMutableDictionary * failedDic = [[YWLogRecordTool shareLogRecordManager] successOrFailedLogDicWith:logDic statusCode:response.statusCode errorInfo:result logTag:logTag];
        [[YWLogRecordTool shareLogRecordManager] saveLocalPatameterLog:failedDic];
        
        if (![[WMHttpTool shareHttpTool] existenceNetwork]) {
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(kTextNoNetwork, nil)];
        }else{
            [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"requestFailed", nil)];
        }
        if (complete) {
            complete(NO);
        }
        YWLog(@"%s,error:%@", __func__, error.localizedDescription);
    }];
}

@end
